home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / Thread Manager Extension 2.0.1 / Sample Applications / Power Examples / SortPicts / Source / WindowObj.cp < prev    next >
Encoding:
Text File  |  1994-06-03  |  10.4 KB  |  477 lines  |  [TEXT/MPS ]

  1. /*************************************************************************************
  2.  *
  3.  *    Object Oriented Shell
  4.  *
  5.  *    WindowObj.cp - C Source
  6.  *
  7.  *      Copyright © Apple Computer, Inc. 1988 - 1993
  8.  *      All rights reserved.
  9.  *
  10.  *   This file defines the basic window functions.  As it turns out, if you
  11.  *     (Yeah, I'm talkin' to you) pull out GWorldObj.cp (& the two sort algorithms)
  12.  *     and modify WindowObj.Link -- you could use the ENTIRE source code here
  13.  *     (Events, Windows, Traps, Main, etc) to support your own applications.
  14.  *     But I won't go into that here.
  15.  *
  16.  *************************************************************************************/
  17.  
  18. #include "WindowObj.h"
  19. #include "main.h"
  20. #include "WindowObj.Link"        // This is your interface file that defines your stuff
  21.  
  22. // Initialize the global
  23. Boolean    WindowObj::isColor = false;
  24.  
  25. //  NewWindowObj
  26. //    This needs to be called immediately after the
  27. //            obj *a = new obj;
  28. //      As in:
  29. //            a->NewWindowObj();
  30.  
  31. Boolean  WindowObj :: NewWindowObj( void)
  32. {
  33.     Rect        boundsRect;
  34.  
  35.  
  36.     
  37.     /*  One of the neat things about the THINK C constructor is it fills the instance
  38.         memory with zerøs which ensures that the variable "me" will be zerø upon startup */
  39.     if( me == NIL)                        //  Each object can only have 1 window
  40.     {                                    //  Unless programmed otherwise...
  41.         SetRect( &boundsRect, 40, 40, 500, 340);
  42.         
  43.         if( isColor == true)
  44.         {
  45.             me = NewCWindow( NIL, &boundsRect, (ConstStr255Param)"\pUntitled", true, 0, (WindowPtr)-1, true, 
  46.                 /* CRITICALLY IMPORTANT:: */   (long) this);
  47.         }
  48.         else
  49.         {
  50.             me = NewWindow( NIL,             //  I have not declared a space for the window
  51.                             &boundsRect,     //    The boundary is 40,40, 140,140 in global coords
  52.         (ConstStr255Param)    "\pUntitled",    //  The title is "Untitled"
  53.                             true,             //  Make it visible
  54.                             0,                //  DocumentProc type window (title, close, zoom, grow)
  55.                             (WindowPtr)-1,        //  Make it the front window
  56.                             true,            //  Give it a goAway box
  57.     /* CRITICAL:: */ (long) this);            //  RefCon:  Give it the address of this object!!
  58.         }
  59.         if( me != NIL)
  60.         {
  61.             SetPort( me);
  62.             ((WindowPeek)me)->windowKind = WindowObjKind;
  63.         }
  64.     }
  65.     if( me == nil)
  66.         return false;
  67.     else
  68.         return true;
  69. }
  70.  
  71.  
  72. /***************************************************************************
  73.  
  74.     InitObj
  75.     
  76.     This function performs basic initialization that all of your windows can use.
  77.     If you have any menus or other resources you want to load in at system start time,
  78.     define a subordinant function of this and then call this function from inside it.
  79.     
  80.  ***************************************************************************/
  81.  void    WindowObj :: InitObj( void)
  82.  {
  83. //     char    objName[32];
  84. //    int        fileRef;
  85. //    int        objNameLen;
  86.     
  87.     isColor = gMac.hasColorQD;        // Retain the color bit
  88.     useActivateClicks = false;        // Do not interpret activate events as action events
  89.  }
  90.  
  91.  
  92. Boolean  WindowObj :: CloseWindowObj( void)
  93. {
  94.     if( me != NIL)                            //    You'll probably overide this code
  95.     {                                        //  With your own that checks the dirtyFlag
  96.         DisposeWindow( me);                    //  Call the Mac to eliminate the window!
  97.         me = NIL;                            //  Take notice that the window is gone.
  98.     }
  99.     return true;
  100. }
  101.  
  102.  
  103. void  WindowObj :: SetWindowTitle( Str255    *title)
  104. {
  105.     SetWTitle( me, (ConstStr255Param)title);
  106. }
  107.  
  108. void  WindowObj :: IdleObj( void)
  109. {
  110.     /* Do nothing during an Idle event ... */
  111. }
  112.  
  113.  
  114. /********************************************************************************
  115.  
  116.     AdjustMenus
  117.     
  118.     If there are any differences in the menu structure of a new Window Object,
  119.     the new object must rewrite this code, there's just no way around it.
  120.  
  121.     However, I encourage authors to copy and paste these elements!!
  122.     (That might help to speed the process.)
  123.  
  124.  ********************************************************************************/
  125.  
  126. void  WindowObj :: AdjustMenusObj( void)
  127. {
  128.     MenuHandle    menu;
  129.  
  130.     menu = GetMHandle( mFile);            // Do the File menu first
  131.     EnableItem(menu, iNew);                // Enable the items, one at a time...
  132.     EnableItem(menu, iOpen);
  133.     if( me != NIL)
  134.     {
  135.         EnableItem(menu, iClose);
  136.         EnableItem(menu, iSave);
  137.         EnableItem(menu, iSaveAs);
  138.         EnableItem(menu, iRevert);
  139.         EnableItem(menu, iPageSetup);
  140.         EnableItem(menu, iPrint);
  141.     }
  142.     else
  143.     {
  144.         DisableItem(menu, iClose);
  145.         DisableItem(menu, iSave);
  146.         DisableItem(menu, iSaveAs);
  147.         DisableItem(menu, iRevert);
  148.         DisableItem(menu, iPageSetup);
  149.         DisableItem(menu, iPrint);
  150.     }
  151.     EnableItem(menu, iQuit);
  152.         
  153.     menu = GetMHandle( mEdit);
  154.     DisableItem(menu, iUndo);
  155.     DisableItem(menu, iCut);
  156.     DisableItem(menu, iCopy);
  157.     DisableItem(menu, iClear);
  158.     DisableItem(menu, iPaste);
  159. }
  160.  
  161.  
  162. void  WindowObj :: KeyPressObj( EventRecord *event)
  163. {
  164.     int a = 0;
  165.     if( a != 0)
  166.         event->modifiers = 0;
  167. }
  168.  
  169.  
  170. void  WindowObj :: MouseClickObj( EventRecord *event)
  171. {
  172.     int        a = 0;
  173.     //  Notice that the shell takes care of:
  174.     //        Dragging, zooming, tracking, etc.
  175.     //  This code only needs to worry about scroll bars,
  176.     //  other controls, and the content region!!
  177.     if( a != 0)
  178.         event->modifiers = 0;
  179. }
  180.  
  181.  
  182. void  WindowObj :: UpdateObj( void)
  183. {
  184.  
  185. #ifdef debugFlag
  186.     DebugStr( "\pUpdate routine in WindowObj");
  187. #endif
  188.     EraseRgn(me->visRgn);    // By default, erase me... then redraw
  189.     DrawObj();    // By default, updating is drawing... until you specify otherwise
  190.                 // But this algorithm is next to lame. So you'll probably want to
  191.                 // write your own UpdateObj() routine.  Just as soon as
  192.                 // you get the rest of your App up and running!
  193. }
  194.  
  195.  
  196. void  WindowObj :: DrawObj( void)
  197. {
  198.  
  199. }
  200.  
  201. void  WindowObj :: PageSetupObj( void)
  202. {
  203. #ifdef debugFlag
  204.     DebugStr( "\pPageSetupObj!!");
  205. #endif
  206.     if( myPrintInfo == (void *)0 )
  207.     {
  208.         myPrintInfo = (THPrint) NewHandle( sizeof( TPrint));
  209.     }
  210.     PrOpen();
  211.     PrStlDialog( myPrintInfo);
  212.     PrClose();
  213. }
  214.  
  215. void  WindowObj :: PrintObj( void)
  216. {
  217.     GrafPtr            saveMe;        // Save the "me" in the case that
  218.                                 // the DrawCode changes GrafPtr's to "me"
  219.                                 // P.S.:  This ONLY works because WaitNextEvent
  220.                                 //          isn't ever called in the middle of
  221.                                 //          printing.  Updating (a possible outcome
  222.                                 //          of W.N.E.) requires "me" to be set right!!
  223.     TPPrPort        myPrintPort;
  224.     
  225.     if( myPrintInfo == (void *)0 )
  226.     {
  227.         PageSetupObj();
  228.     }
  229.     PrOpen();
  230.     if( PrJobDialog( myPrintInfo))
  231.     {
  232.         SetOutlinePreferred( true);    // System 7.0 Support for TrueType
  233.  
  234.         myPrintPort = PrOpenDoc( myPrintInfo, (TPrPort *)0, (Ptr)0);
  235.         if( PrError() == noErr)
  236.         {
  237.             saveMe = me;
  238.             me = (GrafPtr) myPrintPort;
  239.             PrOpenPage( myPrintPort, (Rect *)0);
  240.             if( PrError() == noErr)
  241.             {
  242.                 DrawObj();
  243.             }
  244.             PrClosePage( myPrintPort);
  245.         }
  246.         PrCloseDoc( myPrintPort);    
  247.         me = saveMe;
  248.     }
  249.     PrClose();
  250. }
  251.  
  252. void  WindowObj :: CutObj( EventRecord *event)
  253. {
  254.     long int    oserror;
  255.     int        a = 0;
  256.     if( a != 0)
  257.         event->modifiers = 0;
  258.  
  259.     oserror = ZeroScrap();        // Clear the deskScrap file
  260. }
  261.  
  262. void  WindowObj :: CopyObj( EventRecord *event)
  263. {
  264.     long int    oserror;
  265.     int            a = 0;
  266.     
  267.     if( a != 0)
  268.         event->modifiers = 0;
  269.     
  270.     oserror = ZeroScrap();        // Clear the deskScrap file
  271. }
  272.  
  273.  
  274. void  WindowObj :: PasteObj( EventRecord *event)
  275.     //    No code because I don't know which TYPE the program wants
  276.     int            a = 0;
  277.     if( a != 0)
  278.         event->modifiers = 0;
  279. }
  280.  
  281. void  WindowObj :: UndoObj( EventRecord *event)
  282. {
  283.     //  Undo's need to be handled by the individual window
  284.     int            a = 0;
  285.     if( a != 0)
  286.         event->modifiers = 0;
  287. }
  288.  
  289. void  WindowObj :: DeleteObj( EventRecord *event)
  290. {
  291.     int            a = 0;
  292.     if( a != 0)
  293.         event->modifiers = 0;
  294. }
  295.  
  296. void  WindowObj :: ConvertScrapObj( void)
  297. {
  298.     // Again, nothing here
  299. }
  300.  
  301. void  WindowObj :: ActivateObj( Boolean becomingActive)
  302. {
  303.     if( becomingActive == true)
  304.     {
  305.         if( me != NIL)
  306.         {
  307.             SelectWindow( me);
  308.             this->DrawGrowIconObj();
  309.         }
  310.     }
  311. }
  312.  
  313.  
  314. void  WindowObj :: MenuObj( short menuID, short menuItem, EventRecord *event)
  315. {
  316.     short            b =0;
  317.     WINDOWOBJ       *a;
  318.  
  319.     if( b != 0)
  320.         event->modifiers = 0;
  321.  
  322.     switch( menuID )
  323.     {
  324.         case mFile:
  325.             switch( menuItem)
  326.             {
  327.                 case iNew:
  328.                     a = new WINDOWOBJ;
  329.                     if( a)
  330.                     {
  331.                         a->me = (WindowPtr) 0;
  332.                         if( a->NewWindowObj() == false)
  333.                             delete a;
  334.                     }
  335.                     break;
  336.                 case iClose:
  337.                     CloseWindowObj();
  338.                     break;
  339.                 case iPageSetup:
  340.                     PageSetupObj();
  341.                     break;
  342.                 case iPrint:
  343.                     PrintObj();
  344.                     break;
  345.                 default:
  346.                     AppMenu( menuID, menuItem);        // This is like inheritance!!
  347.                     break;
  348.             }
  349.             break;
  350.             
  351.         default:
  352.             AppMenu( menuID, menuItem);                // This is like inheritance!!
  353.             break;
  354.     }
  355.     HiliteMenu(0);
  356. }
  357.  
  358.  
  359. void  WindowObj :: AdjustCursorObj( Point mouseLoc, RgnHandle cursorRgn)
  360. {
  361.     /* This routine doesn't use the mouseLocation, but your windows can! */
  362.     if( cursorRgn != NIL)        // From your window code, you can call AdjustCursor
  363.     {                            // with NIL and this routine will simply adjust the cursor
  364.         SetEmptyRgn( cursorRgn);// you should try incorporating it into your code, too.
  365.     }
  366.     SetCursor( &qd.arrow);
  367.     if( mouseLoc.h == -5000)
  368.         ;
  369. }
  370.  
  371.  
  372.  
  373.  
  374. void  WindowObj :: DrawGrowIconObj( void)
  375. {
  376.     WindowPtr    copyMe = me;
  377.     
  378.     if( me != NIL)
  379.     {
  380.         DrawGrowIcon( me);
  381.     }
  382. }
  383.  
  384. void    WindowObj :: DragWindowObj( EventRecord *event)
  385. {
  386.     DragWindow( me, event->where, &qd.screenBits.bounds);
  387. }
  388.  
  389.  
  390. void    WindowObj :: GrowWindowObj( EventRecord *event)
  391. {
  392.     Rect        growRect;
  393.     long        newSize;
  394.  
  395.     growRect = qd.screenBits.bounds;
  396.     growRect.top = growRect.left = 80;        /* Arbitrary minimum size */
  397.     newSize = GrowWindow( me, event->where, &growRect);
  398.     if( newSize != 0)
  399.     {
  400.         InvalidateScrollbars( me);
  401.         SizeWindow( me, LoWrd(newSize), HiWrd(newSize), true);
  402.         InvalidateScrollbars( me);
  403.     }
  404. }
  405.  
  406.  
  407. void    WindowObj :: ZoomWindowObj( EventRecord *event, Boolean OutIsTrue)
  408. {
  409.     if( event->modifiers == 0x8000)
  410.         OutIsTrue = true;
  411. }
  412.  
  413.  
  414. void    WindowObj :: ScrollWindowObj( EventRecord *event)
  415. {
  416.     if( event->modifiers == 0x8000)
  417.         ;
  418. }
  419.  
  420.  
  421. void    WindowObj :: DiskInsertObj( EventRecord *event)
  422. {
  423.     Point            aPoint = {100, 100};
  424.         if( HiWrd(event->message) != noErr)
  425.         {
  426.             (void) DIBadMount(aPoint, event->message);
  427.         }
  428. }
  429.  
  430. short int    WindowObj :: DrawingWidth( void)
  431. {
  432.     if( me)
  433.     {
  434.         short int        width = me->portRect.right - me->portRect.left;
  435.         WindowPeek        winObj = (WindowPeek)me;
  436.         short int        myKind = winObj -> windowKind;
  437.         
  438.         if( myKind == 0 || myKind == 8)
  439.             return width - 16;
  440.         else
  441.             return    width;
  442.     }
  443.     else
  444.         return 0;
  445. }
  446.  
  447. short int    WindowObj :: DrawingHeight( void)
  448. {
  449.     if( me)
  450.     {
  451.         short int        height = me->portRect.bottom - me->portRect.top;
  452.         WindowPeek        winObj = (WindowPeek)me;
  453.         short int        myKind = winObj -> windowKind;
  454.         
  455.         if( myKind == 0 || myKind == 8)
  456.             return height - 16;
  457.         else
  458.             return    height;
  459.     }
  460.     else
  461.         return 0;
  462. }
  463.  
  464. void    WindowObj :: DrawingRect( Rect *area)
  465. {
  466.     if( me)
  467.     {
  468.         *area = me -> portRect;
  469.         area->right = area->left + DrawingWidth();
  470.         area->bottom = area->top + DrawingHeight();
  471.     }
  472.     else
  473.         SetRect( area, 0, 0, 0, 0);
  474. }
  475.  
  476.